<template>
  <div class="dateTime">
    <el-form-item :label="title + '：'">
      <el-date-picker
        v-model="dateTime"
        type="datetimerange"
        clearable
        :default-time="['00:00:00', '23:59:59']"
        value-format="yyyy-MM-dd HH:mm:ss"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        @change="handleValue"
      >
      </el-date-picker>
    </el-form-item>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'dateTime',
  inheritAttrs: false,
  props: {
    title: {
      type: String,
      default: '时间'
    },
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dateTime: []
    }
  },
  methods: {
    handleValue() {
      let params = {
        types: [],
        datas: [this.dateTime[0], this.dateTime[1], this.dateTime]
      }
      switch (this.type) {
        case '2': // 发布时间
          params = {
            issuedataStart: this.dateTime[0],
            issuedataEnd: this.dateTime[1],
            issuedata: this.dateTime
          }
          break
        case '3': // 索引时间
          params = {
            indexesdataStart: this.dateTime[0],
            indexesdataEnd: this.dateTime[1],
            indexesdata: this.dateTime
          }
          break
      }
      this.setNewFormData(params)
    },
    init() {
      switch (this.type) {
        case '2': // 发布时间
          this.dateTime = [
            this.formData.issuedataStart,
            this.formData.issuedataEnd
          ]
          break
        case '3': // 索引时间
          this.dateTime = [
            this.formData.indexesdataStart,
            this.formData.indexesdataEnd
          ]
          break
      }
    },
    ...mapMutations('charts', ['setNewFormData'])
  },
  computed: {
    ...mapState('charts', ['formData', 'chartsCondition', 'resetFormData'])
  },
  mounted() {
    this.init()
  },
  watch: {
    resetFormData() {
      this.init()
    }
  }
}
</script>

<style scoped></style>
